//  ========== 
//  = 产品列表 业务JS = 
//  ==========
var page1 = 1;
var page2 = 1;
var page3 = 1;
var index = 0
var Main = function(){
	mui.init({
		swipeBack:true 		//启用右滑关闭功能
	});

    index = Util.requestURL('index') | 0;
	mui('.mui-slider').slider().gotoItem(index,0);

	this.init();
	this.loadData(index,1);
	
};
Main.prototype = {

	init : function(){
		mui('.mui-scroll-wrapper').scroll({
					indicators: true //是否显示滚动条
		});
		var _this = this;
		mui.each(document.querySelectorAll('.mui-slider-group .mui-scroll'), function(index, pullRefreshEl) {
			mui(pullRefreshEl).pullToRefresh({
				down: {
					callback: function() {
					   var self = this;
					   if(index == 0 ){
                           page1=1;					   	
					   }
					   if(index == 1 ){
                           page2=1;	
					   }
					   if(index == 2 ){
                           page3=1;					   	
					   }
						_this.loadData(index,1,function(){
							_this._prodBindTap()
							self.endPullDownToRefresh();
						});
					}
				},
				up: {
					callback: function() {
						var self = this;
						var page ;
						if(index == 0 ){
                           ++page1;	
                           page = page1; 
					    }
					    if(index == 1 ){
                           ++page2;		
                           page = page2;
					    }
					    if(index == 2 ){
                           ++page3;
                           page = page3;
					    }
						_this.loadData(index,page,function(){
							_this._prodBindTap();
							self.endPullUpToRefresh();
						});
	
					}
				}
			});
	 	});
					
		
		_this.eventInit();

	},
	eventInit : function(){//绑定事件
		var _this = this;
        $(".mui-control-item").each(function(index){
        	this.addEventListener('tap',function(){
        		page = 1;
        		_this.loadData(index,page);
        	});
        });
	},
	_prodBindTap : function(index,param){//列表绑定点击事件,index:0-点点安盈 1-点点专享 2-债券转让
		if(index == 0){
			//点点安盈
			mui("#G-proj-bid-plan").on('tap','li',function(){
				mui.openWindow({
					    url:'bidPlanDetail.html?bidId='+this.getAttribute('data-id'),
					    id:'bidPlanDetail',
					    extras:param
				     }
				);
			});
		}else if(index == 1){//点点专享
			mui("#G-proj-bid-sale").on('tap','li',function(){
				mui.openWindow({
					    url:'bidSaleDetail.html?mmplanId='+this.getAttribute('data-id'),
					    id:'bidSaleDetail',
					    extras:param
				     }
				);
			});
		}
	},

	//项目列表
	loadData : function(index,page,_callback){
		var _this = this;
		var param = {
				isMobile:1,
				page:page,
				limit:5
				};
		var url;
		var currTab;
        if(index == 0){//点点安盈 
       	     url = server_url + "/creditFlow/financingAgency/listPlBidPlan.do";
       	     currTab = document.getElementById('G-proj-bid-plan');
		}else if(index == 1){//点点专享
			 url = server_url + "/creditFlow/financingAgency/allPlmanagemoneyPlBidSale.do";
			 currTab = document.getElementById('G-proj-bid-sale');
		}
		$.getJSON(url,param  ,function(data){
			var allHtml = '';
            if(data.success){
            	if(page == 1){
            	 currTab.innerHTML = '';
                }
            	if(index ==0 ){
	            	for(var i=0;i<data.result.length;i++){
	            		var item = data.result[i];
	            		 var _html = '<li class="mui-table-view-cell" data-id="'+item.bidId+'">'; 
							if(i==0&&page==1){
											/**右上角状态 beg*/
							_html += '\n <div class="m-proj-rt-tips">';
							_html += '\n <i class="mui-icon u-iconfont">&#xe683;</i>';
							_html += '\n </div>';
											/**右上角状态 end*/
							}
							_html += '\n <div class="mui-table">';
							_html += '\n <div class="mui-table-cell mui-col-xs-10">';
							_html += '\n <h4 class="mui-ellipsis m-proj-tit">'+item.bidProName+'</h4>';
							_html += '\n </div>';
							_html += '\n <div class="mui-table-cell mui-col-xs-2 mui-text-right">';
							if('1'==item.coupon){
							_html += '\n <span class="mui-h5 m-proj-tag tag-yew">券</span>';	
							}
							if('1'==item.novice){
							_html += '\n <span class="mui-h5 m-proj-tag tag-ble">新</span>';
							}
							_html += '\n </div>';
							_html += '\n </div>';
							_html += '\n <div class="m-proj-info">';
							_html += '\n <div class="m-proj-box m-proj-lt">';
							_html += '\n <div class="m-info-top">';
							_html += '\n <span class="m-big">'+item.interestRate+'</span>';
							if(item.addRate && item.addRate > 0){
							_html += '\n <span class="m-small">+'+item.addRate+'</span>';	
							}
							
							_html += '\n <span class="m-unit">%</span>';
							_html += '\n </div>';
							_html += '\n <div class="m-info-des">年化利率</div>';
							_html += '\n </div>';
							_html += '\n <div class="m-proj-box m-proj-cn">';
							_html += '\n <div class="m-info-top">';
							_html += '\n <span class="m-big">'+item.loanLife+'</span>';
							/*_html += '\n <span class="m-unit">个月</span>';*/
							_html += '\n </div>';
							_html += '\n <div class="m-info-des">项目期限</div>';
							_html += '\n </div>';
							_html += '\n <div class="m-proj-box m-proj-rt">';
							_html += '\n <div class="m-proj-state-p" data-progress="'+item.progress+'" >';
							_html += '\n </div>';
							_html += '\n </div>';
							_html += '\n </div>';
							_html += '\n <div class="mui-table m-proj-nottom">';
							_html += '\n <div class="mui-table-cell mui-col-xs-5">';
							_html += '\n <h4 class="mui-ellipsis m-proj-tit">';
							_html += '\n <i class="mui-icon u-iconfont">&#xe600;</i>';
							_html += item.bidMoney+'\n </h4>';
							_html += '\n </div>';
							_html += '\n <div class="mui-table-cell mui-col-xs-5 mui-text-right">';
							_html += '\n <span class="mui-h5 m-proj-tag">'+item.theWayBack+'</span>';
							_html += '\n </div>';
							_html += '\n </div>';
							_html += '\n </li>';
										
							//allHtml+=_html;
							currTab.innerHTML += _html;

					}	
            	}else if(index == 1 ){
	            	for(var i=0;i<data.result.length;i++){
	            		var item = data.result[i];
	            		 var _html = '<li class="mui-table-view-cell" data-id="'+item.mmplanId+'">'; 
							if(i==0&&page==1){
											/**右上角状态 beg*/
							_html += '\n <div class="m-proj-rt-tips">';
							_html += '\n <i class="mui-icon u-iconfont">&#xe683;</i>';
							_html += '\n </div>';
											/**右上角状态 end*/
							}
							_html += '\n <div class="mui-table">';
							_html += '\n <div class="mui-table-cell mui-col-xs-10">';
							_html += '\n <h4 class="mui-ellipsis m-proj-tit">'+item.mmName+'</h4>';
							_html += '\n </div>';
							_html += '\n <div class="mui-table-cell mui-col-xs-2 mui-text-right">';
							if('1'==item.coupon){
							_html += '\n <span class="mui-h5 m-proj-tag tag-yew">券</span>';	
							}
							if('1'==item.novice){
							_html += '\n <span class="mui-h5 m-proj-tag tag-ble">新</span>';
							}
							_html += '\n </div>';
							_html += '\n </div>';
							_html += '\n <div class="m-proj-info">';
							_html += '\n <div class="m-proj-box m-proj-lt">';
							_html += '\n <div class="m-info-top">';
							_html += '\n <span class="m-big">'+item.yeaRate+'</span>';
							if(item.addRate && item.addRate > 0){
							_html += '\n <span class="m-small">'+item.addRate+'</span>';	
							}
							
							_html += '\n <span class="m-unit">%</span>';
							_html += '\n </div>';
							_html += '\n <div class="m-info-des">年化利率</div>';
							_html += '\n </div>';
							_html += '\n <div class="m-proj-box m-proj-cn">';
							_html += '\n <div class="m-info-top">';
							_html += '\n <span class="m-big">'+item.investlimit+'天</span>';
							/*_html += '\n <span class="m-unit">个月</span>';*/
							_html += '\n </div>';
							_html += '\n <div class="m-info-des">项目期限</div>';
							_html += '\n </div>';
							_html += '\n <div class="m-proj-box m-proj-rt">';
							_html += '\n <div class="m-proj-state-p" data-progress="'+item.progress+'" >';
							_html += '\n </div>';
							_html += '\n </div>';
							_html += '\n </div>';
							_html += '\n <div class="mui-table m-proj-nottom">';
							_html += '\n <div class="mui-table-cell mui-col-xs-5">';
							_html += '\n <h4 class="mui-ellipsis m-proj-tit">';
							_html += '\n <i class="mui-icon u-iconfont">&#xe600;</i>';
							_html += item.sumMoney+'\n </h4>';
							_html += '\n </div>';
							_html += '\n <div class="mui-table-cell mui-col-xs-5 mui-text-right">';
							_html += '\n <span class="mui-h5 m-proj-tag">'+item.guaranteeWay+'</span>';
							_html += '\n </div>';
							_html += '\n </div>';
							_html += '\n </li>';
										
							//allHtml+=_html;
							
							currTab.innerHTML += _html;
					}	
            	}
            
	            //设置满字、抢字
	            $('.m-proj-state-p').each(function(){
	            	$(this).html('');
	            	var progress = this.getAttribute('data-progress');
	            	$(this).radialIndicator({
						initValue: progress,
						radius: 22,
						barWidth: 2,
    					barColor: '#48adfe',
		                format: function(){
		                	if(progress == '100'){
						 		return '满';
		                	}else{
		                		return '抢';
		                	}
						}
					});
	            });
            
            }			
            
            if(_this._prodBindTap){
			_this._prodBindTap(index,{});
            }
             
             if($.isFunction(_callback)){
             	_callback();
             }
		});
	}
	
}
